<template>
  <div>
    <div class="box1">
      <van-list
        v-model="loading"
        :finished="finished"
       
        @load="onLoad"
      >
          <van-coupon-list
                    :coupons="data"
                    
                  
                    :chosen-coupon="chosenCoupon"
                    @change="onChange"
                    @exchange="onExchange"
                    :show-count="false"
                    :show-exchange-bar="false"
                    close-button-text="领取"
                    :show-close-button="false"
                />
      
     
      </van-list>
    </div>

    <div class="buts">
               <van-button @click="sub()" >领取</van-button>
           </div>
  </div>
</template>
<script>
let t;
import {hideWxMenu } from '@/assets/js/tools'
import { mapState,mapActions } from "vuex";
export default {
  name: "home",
  data() {
    return {
      count:0,
      data:[],
      params:{
        page:0,
        limit:20,
      },
      chosenCoupon:-1,
     
      loading: false,
      finished: false,
      openid:'',
    };
  },
  computed: {
    ...mapState(['usersDetail']),
  },
  methods: { 
    ...mapActions([
				'GetUsersDetail',
			]),
    sub(){
      if(t.chosenCoupon<0){
          return t.$toast('请选择优惠券后在提交')
      }

      let d={
          openid:localStorage.getItem('openid'),
          id:t.data[t.chosenCoupon].value
      }
      t['$API']['userReceive_coupons'](d).then(res=>{
            if(res.code==200){
                t.$toast(res.msg)
                setTimeout((t) => {
                    
                           window.location.reload()
                     
                }, 500,t);
            }
      }) 
    },
      onChange(...item) {
        console.log("item1",item)
      this.chosenCoupon = item[0];
    },
    onExchange(...item){
      console.log("item2",item) 
    },
    onLoad(){
       let t=this;
      this.params.page++;
    
      
  
      this.getList();
    },
    getList() {
      t.params.openid=localStorage.getItem('openid');
      t["$API"]["userHomeCoupons"](t.params).then((res) => {
         this.loading = false;
            if(res.code==200){
              t.count=res.data.count

              if(t.params.page==1){
                t.data=[];
                 res.data.data.forEach(elem => {
                   let startime=new Date(((elem.startime).replace(/-/g,'/'))).getTime(); 
                   let endtime=new Date(((elem.endtime).replace(/-/g,'/'))).getTime(); 
                           t.data.push( {
                              available: 1,
                            value: elem.id,
                             reason: '',
                            condition:elem.desc,
                            valueDesc:elem.money, 
                            startAt:startime,
                            endAt:endtime,
                            name:elem.title,
                            unitDesc:'元'
                          })
                    });
                    console.log(" t.data", t.data)
              }else{
                  res.data.data.forEach(elem => {
                   let startime=new Date(((elem.startime).replace(/-/g,'/'))).getTime(); 
                   let endtime=new Date(((elem.endtime).replace(/-/g,'/'))).getTime(); 
                           t.data.push( {
                              available: 1,
                            value: elem.id,
                             reason: '',
                            condition:elem.desc,
                            valueDesc:elem.money, 
                            startAt:startime,
                            endAt:endtime,
                            name:elem.title,
                            unitDesc:'元'
                          })
                    });
              
              }
             
              if (t.data.length >=t.count) {
                this.finished = true;
              }
            }else{
               t.data=[];
          this.finished = true;
            }
      });
    },
  },
  mounted() {
    t = this;
    t.openid=localStorage.getItem('openid')


     hideWxMenu()
    
       
         if(!t.usersDetail.id){
        
            t.GetUsersDetail({openid:localStorage.getItem('openid')})  
          }
  },
};
</script>

<style lang="less" scoped>
/deep/ .van-tabs__wrap{
  display: none;
}

 .buts{
      display: flex;
      position: fixed;
      bottom: 1rem;
      width: 100%;
      justify-content: center;
      >button{
          width: 90%;
          background-color: #376157;
             background-image: linear-gradient(45deg, #376157, #00b894);
          color: white;
           
      }
  }

  /deep/ .van-coupon__head{
    max-width: 10rem;
    width: 8rem;
  }
</style>
